home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 228_01 / lj.c < prev    next >
Text File  |  1987-07-29  |  6KB  |  230 lines

  1. /*
  2. HEADER:         CUGXXX;
  3. TITLE:          HP LaserJet Printing Utility;
  4. DATE:           3-20-86;
  5. DESCRIPTION:    Printing utility for the HP LaserJet;
  6. KEYWORDS:       Laser printer;
  7. FILENAME:       LJ.C;
  8. WARNINGS:       None;
  9. AUTHORS:        Joe Barnhart, Ray Duncan, Abel DeSouza;
  10. COMPILER:       C-86;
  11. REFERENCES:     US-DISK 1307, Dr. Dobbs Journal, 9-85, p. 117;
  12. ENDREF
  13. */
  14. /**
  15.  **
  16.  **        LJ.C -- A printing utility for the HP LaserJet
  17.  **        This program prints a series of files on the LaserJet
  18.  **        printer.  The files are printed in a "landscape" font at
  19.  **        17 characters to the inch.  To take advantage of this
  20.  **        density, two "pages" of information from the file are
  21.  **        printed on each piece of paper (left and right halves).
  22.  **
  23.  **        Usage is:        LJ  [-b] file1 file2 file3 ...
  24.  **                            -b ignore page breaks.
  25.  **
  26.  **        Where file# is a valid MS-DOS filename, included on the
  27.  **        command line.
  28.  **
  29.  **        Reference Dr. Dobbs Journal, Sept 1985 p117
  30.  **        Joe Barnhart    original version        May 5, 1985
  31.  **        Ray Duncan        date and time stamping    May 27, 1985
  32.  **        Joe Barnhart    revised date stamping    June 6, 1985
  33.  **        Abel DeSouza    revised for LaserJet cartridge 92286A & CI86
  34.  **                                Sept 16, 1985
  35.  **        revised for C86 and & page breaks option 10/9/85
  36.  **
  37.  */
  38.  
  39. #include <f:stdio.h>
  40.  
  41. #define MAXLINE 55            /* maximum lines per page */
  42. #define PAGE    12        /* ^L MS DOS Page break   */
  43. #define TAB        4            /* width of one tab stop */
  44. #define FALSE    0
  45. #define TRUE    1
  46.  
  47. typedef struct {
  48.         int ax, bx, cx, dx, si, di, ds, es;
  49.     } REGSET;
  50.  
  51. main(argc, argv)
  52.     int        argc;
  53.     char    *argv[];
  54.     {
  55.  
  56.     char    c, page, *arg;
  57.     int    filenum, i;
  58.     FILE    *fp, *prn, *fopen();
  59.  
  60.     /* set defaults */
  61.     page = FALSE;
  62.     i = 1;
  63.  
  64.     if ( ( prn = fopen( "PRN", "w" ) ) == NULL)
  65.         printf( "Error opening printer as file.\n" );
  66.  
  67.     else if (argc != 1) {
  68.         if (*(arg = *++argv) == '-') {
  69. /*            printf("argument string: %s \n", arg);    */
  70.             switch (c = *++arg, toupper(c)) {
  71.                 case 'B' :
  72.                     page = TRUE;
  73.                     i = 2;
  74.                     break;
  75.                 default:
  76.                     user_err(1);
  77.                 }
  78.             }
  79.         argv--;
  80. /*        printf("argc = %d, i = %d\n", argc, i); */
  81.         if (argc > i) {
  82.             /* initialize the LaserJet for landscape printing */
  83.             fprintf( prn, "\033E\033&l1O\033(8U\033(s0p16.66h8.5v0s-1b0T" );
  84.             for( filenum = i; filenum < argc; filenum++ )  {
  85.                 fp = fopen( argv[filenum], "r" );
  86.                 if( fp == NULL )
  87.                     printf( "File %s doesn't exist.\n", argv[filenum] );
  88.                 else {
  89.                     printf( "Now printing %s\n", argv[filenum] );
  90.                     printfile( fp, prn, argv[filenum], page );
  91.                     fclose( fp );
  92.                     }
  93.                 }
  94.             fprintf( prn, "\015\033E" );        /* clear LaserJet */
  95.             }
  96.         else
  97.             user_err(2);
  98.         }
  99.     else
  100.         user_err(0);
  101.     }
  102.  
  103. /***************************************************************/
  104.  
  105. user_err(sw)
  106.     int sw;
  107.     {
  108.     printf("\nERROR: Invalid parameter specification\n\n");
  109.     printf("Usage: lj <-b> <filename> <filename>...\n");
  110.  
  111.     switch(sw) {
  112.         case 1:
  113.             printf("Wrong flag specified");
  114.             break;
  115.         case 2:
  116.             printf("File name required");
  117.             break;
  118.         default :
  119.             printf("Flags: -b  Ignore page breaks\n");
  120.         }
  121.     exit(0);
  122.     }
  123.  
  124. /***************************************************************/
  125.  
  126. printfile( fp, prn, filename, page)
  127.     FILE    *fp, *prn;
  128.     char    *filename, page;
  129.     {
  130.  
  131.     int pagenum = 1;
  132.  
  133.     while( !feof( fp ) ) {
  134.         fprintf( prn, "\033&a0r85m5L\033&l8D\015" );        /* set left half */
  135.         printpage( fp, prn );                        /* print page */
  136.         if ( !feof( fp ) ) {                        /* if more .. */
  137.             fprintf( prn,"\033&a0r171m91L\033&l8D" );        /* set right half */
  138.             printpage( fp, prn, page );                    /* print another */
  139.             }
  140.         stamp( prn, filename, pagenum++ );            /* title */
  141.         fputc( PAGE, prn );                            /* kick paper */
  142.         }
  143.     }
  144.  
  145. /***************************************************************/
  146.  
  147. printpage( fp, prn, page)
  148.     FILE    *fp, *prn;
  149.     char    page;
  150.     {
  151.  
  152.     char    c;
  153.     int        line, col;
  154.  
  155.     line = col = 0;
  156.     while( line < MAXLINE )
  157.         switch( c = fgetc(fp) ) {
  158.             case '\n':                /* newline found */
  159.                 col = 0;            /* zero column */
  160.                 line++;             /* adv line cnt */
  161.                 fputc( '\n', prn);
  162.                 break;
  163.             case '\t':                /* tab found */
  164.                 do
  165.                     fputc( '\040', prn);
  166.                 while ( (++col % TAB ) != 0 );
  167.                 break;
  168.             case PAGE:                /* page break or */
  169.                 if (page) break;
  170.             case EOF:            /* EOF found */
  171.                 line = MAXLINE;     /* force terminate */
  172.                 break;
  173.             default:                /* no special case */
  174.                 fputc( c, prn );    /* print character */
  175.                 col++;
  176.                 break;
  177.             }
  178.     }
  179.  
  180. /***************************************************************/
  181.  
  182. stamp( prn, filename, pagenum )
  183.     FILE    *prn;
  184.     char    *filename;
  185.     {
  186.  
  187.     char    datestr[10], timestr[10];
  188.  
  189.     fprintf(prn, "\033&a171m51L" );            /* widen margins */
  190.     fprintf(prn, "\015\033&a57R" );            /* move to row 58 */
  191.     fprintf(prn, "File: %-83s", filename );
  192.     fprintf(prn, "Page %-3d", pagenum);
  193.     timestamp( timestr );
  194.     datestamp( datestr );
  195.     fprintf( prn, "   %s   %s", datestr, timestr);
  196.     }
  197.  
  198. /***************************************************************/
  199.  
  200. datestamp( datestr )
  201.     char    *datestr;
  202.     {
  203.  
  204.     REGSET    regs;
  205.     int month, day, year;
  206.  
  207.     regs.ax = 0x2a00;
  208.     sysint( 0x21, ®s, ®s );
  209.     month = ( regs.dx >> 8 ) & 255;
  210.     day      = regs.dx & 255;
  211.     year  = regs.cx - 1900;
  212.     sprintf( datestr, "%02d/%02d/%02d", month, day, year );
  213.     }
  214.  
  215. /***************************************************************/
  216.  
  217. timestamp( timestr )
  218.     char    *timestr;
  219.     {
  220.  
  221.     REGSET    regs;
  222.     int hours, mins;
  223.  
  224.     regs.ax = 0x2c00;
  225.     sysint( 0x21, ®s, ®s );
  226.     hours = ( regs.cx >> 8 ) & 255;
  227.     mins  = regs.cx & 255;
  228.     sprintf( timestr, "%02d:%02d", hours, mins );
  229.     }
  230.